package teleradiologia.repository;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.security.providers.encoding.BaseDigestPasswordEncoder;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import teleradiologia.domain.Managerdiagnosticlab;
import teleradiologia.utils.HibernateUtil;

public class ManagerdiagnosticlabDaoImpl implements ManagerdiagnosticlabDao {

	private BaseDigestPasswordEncoder encoder = new Md5PasswordEncoder();

	@Override
	public String create(Managerdiagnosticlab managerdiagnosticlab) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		managerdiagnosticlab.getPerson().setPassword(
				encoder.encodePassword(managerdiagnosticlab.getPerson()
						.getPassword(), managerdiagnosticlab.getPerson()
						.getUsername()));

		String nip = (String) session.save(managerdiagnosticlab);

		tx.commit();
		session.close();

		return nip;
	}

	@Override
	public void delete(Managerdiagnosticlab managerdiagnosticlab) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(managerdiagnosticlab);

		tx.commit();
		session.close();
	}

	@Override
	public Managerdiagnosticlab getByNip(String nip) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Managerdiagnosticlab managerdiagnosticlab = (Managerdiagnosticlab) session
				.get(Managerdiagnosticlab.class, nip);

		tx.commit();
		session.close();

		return managerdiagnosticlab;
	}

	@Override
	public void update(Managerdiagnosticlab managerdiagnosticlab) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		managerdiagnosticlab.getPerson().setPassword(
				encoder.encodePassword(managerdiagnosticlab.getPerson()
						.getPassword(), managerdiagnosticlab.getPerson()
						.getUsername()));

		session.update(managerdiagnosticlab);

		tx.commit();
		session.close();

	}

	@Override
	public Managerdiagnosticlab getByIdPerson(long idPerson) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Managerdiagnosticlab managerdiagnosticlab = (Managerdiagnosticlab) session
				.createQuery(
						"from Managerdiagnosticlab managerdiagnosticlab "
								+ " where managerdiagnosticlab.person.id = :idPerson")
				.setLong("idPerson", idPerson).uniqueResult();

		tx.commit();
		session.close();

		return managerdiagnosticlab;
	}

	@Override
	public Managerdiagnosticlab getByKrsnumber(String krsnumber) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Managerdiagnosticlab managerdiagnosticlab = (Managerdiagnosticlab) session
				.createQuery(
						"from Managerdiagnosticlab managerdiagnosticlab "
								+ " where managerdiagnosticlab.diagnosticlab = :krsnumber")
				.setString("krsnumber", krsnumber).uniqueResult();

		tx.commit();
		session.close();

		return managerdiagnosticlab;
	}
}
